/*
 * morestdlib.c
 *
 *  Created on: Sep 6, 2008
 *      Author: pierre
 */
#include <stdio.h>
#include "lang/morestddef.h"
#include "lang/morestdlib.h"
#include "util/debug.h"

void exitAt(const char* filename,int line,int status)
	{
	if(status!=0)
		{
		fprintf(stderr,"Exiting at \"%s\":%d with status(%d)",
				(filename==NULL?"?":filename),
				line,
				status
				);
		}
	exit(status);
	}

Ptr Calloc(size_t nmemb, size_t size)
	{
	Ptr ptr= calloc(nmemb,size);
	if(ptr==NULL)
		{
		DEBUG3("OutOfMemory. Calloc(%d,%d)",nmemb,size);
		}
	return ptr;
	}

Ptr Malloc(size_t size)
	{
	Ptr ptr= malloc(size);
	if(ptr==NULL)
		{
		DEBUG2("OutOfMemory. Malloc(%d)",size);
		}
	return ptr;
	}

void  Free(Ptr ptr)
	{
	free(ptr);
	}

Ptr Realloc(Ptr ptr, size_t size)
	{
	Ptr p=realloc(ptr,size);
	if(p==NULL && ptr!=null && size!=0)
		{
		DEBUG2("OutOfMemory. Realloc(%d)",size);
		}
	return p;
	}

Ptr safeCalloc(size_t nmemb, size_t size)
	{
	Ptr ptr= Calloc(nmemb,size);
	if(ptr==NULL)
		{
		EXIT(-1);
		}
	return ptr;
	}

Ptr safeMalloc(size_t size)
	{
	Ptr ptr= Malloc(size);
	if(ptr==NULL)
		{
		EXIT(-1);
		}
	return ptr;
	}


Ptr safeRealloc(Ptr ptr, size_t size)
	{
	Ptr p=Realloc(ptr,size);
	if(p==NULL && ptr!=null && size!=0)
		{
		EXIT(-1);
		}
	return p;
	}
